package com.fudan.adse.service.impl;

import com.fudan.adse.domain.po.FilmRowPiece;
import com.fudan.adse.domain.po.FilmRowPieceExample;
import com.fudan.adse.grpc.*;
import com.fudan.adse.mbg.mapper.FilmMapper;
import com.fudan.adse.mbg.mapper.FilmRowPieceMapper;
import com.fudan.adse.utils.DateTimeUtil;
import com.google.protobuf.Empty;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author Jerry Zhang
 * create: 2022-11-24 15:52
 */
@Slf4j
@GrpcService
public class FilmGrpcServiceImpl extends FilmGrpc.FilmImplBase {
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private FilmRowPieceMapper rowPieceMapper;

    /**
     * 获取某影院的场次数据、某影片的场次数据
     *
     * @return
     */
    @Override
    public void listRowPiecesByCinemaOrFilm(RowPieceQuery request, StreamObserver<ListResponse> responseObserver) {
        FilmRowPieceExample example = new FilmRowPieceExample();
        FilmRowPieceExample.Criteria criteria = example.createCriteria();
        if (request.getRowPieceId() > 0) {
            criteria.andIdEqualTo(request.getRowPieceId());
        } else {
            if (request.getCinemaId() > 0) {
                criteria.andCinemaIdEqualTo(request.getCinemaId());
            }
            if (request.getFilmId() > 0) {
                criteria.andFilmIdEqualTo(request.getFilmId());
            }
            if (!request.getSince().isEmpty()) {
                criteria.andEndTimeGreaterThan(DateTimeUtil.stringToDate(request.getSince()));
            }
            if (!request.getUntil().isEmpty()) {
                criteria.andStartTimeLessThan(DateTimeUtil.stringToDate(request.getUntil()));
            }
        }
        List<Integer> rowPieceIds = new ArrayList<>();
        rowPieceMapper.selectByExample(example).forEach(rowPiece -> rowPieceIds.add(rowPiece.getId()));
        responseObserver.onNext(ListResponse.newBuilder()
                .addAllId(rowPieceIds)
                .build());
        responseObserver.onCompleted();
    }

    /**
     * 获取某排片场次详细数据
     *
     * @return
     */
    @Override
    public void getRowPieceById(RowPieceQuery request, StreamObserver<RowPieceResponse> responseObserver) {
        FilmRowPiece rowPiece = rowPieceMapper.selectByPrimaryKey(request.getRowPieceId());
        responseObserver.onNext(RowPieceResponse.newBuilder()
                .setId(rowPiece.getId())
                .setCinemaId(rowPiece.getCinemaId())
                .setFilmId(rowPiece.getFilmId())
                .setRoomId(rowPiece.getRoomId())
                .setStartTime(DateTimeUtil.format(rowPiece.getStartTime()))
                .setEndTime(DateTimeUtil.format(rowPiece.getEndTime()))
                .setRemainCapacity(rowPiece.getRemainCapacity())
                .setPrice(rowPiece.getPrice())
                .build());
        responseObserver.onCompleted();
    }

    @Override
    public void updateRemainCapacity(SoldSeatsQuery request, StreamObserver<Empty> responseObserver) {
        FilmRowPiece rowPiece = rowPieceMapper.selectByPrimaryKey(request.getRowPieceId());
        rowPiece.setRemainCapacity(rowPiece.getRemainCapacity() - request.getCount());
        rowPieceMapper.updateByPrimaryKeySelective(rowPiece);
        responseObserver.onNext(Empty.newBuilder().build());
        responseObserver.onCompleted();
    }
}
